package cn.wolfcode.plus.service.impl;

import cn.wolfcode.plus.domain.Employee;
import cn.wolfcode.plus.domain.Role;
import cn.wolfcode.plus.exception.LoginException;
import cn.wolfcode.plus.mapper.EmployeeMapper;
import cn.wolfcode.plus.qo.PageResult;
import cn.wolfcode.plus.qo.QueryObject;
import cn.wolfcode.plus.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Author wyy
 * Date 2021/2/8 18:21
 **/
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee>
        implements IEmployeeService {
    @Override
    public PageResult<Employee> query(QueryObject qo) {
        return new PageResult<>(this.baseMapper.selectForList(new Page<>(qo.getCurrentPage(),qo.getPageSize()),null));
    }

    @Override
    public Employee checkLogin(String username, String password) {
        if (!StringUtils.hasText(username)) {

            throw new LoginException("用户名不能为空");
        }
        if (!StringUtils.hasText(password)) {

            throw new LoginException("密码不能为空");
        }

        //判断是否有这个账号
        Employee employee = this.baseMapper.checkLogin(username, password);
        if (employee == null) {
            throw new LoginException("用户名或密码错误");
        }
        return employee;
    }

    @Override
    public List<String> getAllPermission(Long id) {
        return this.baseMapper.selectPermissionsByEId(id);
    }

    @Override
    public List<Role> getAllRoles(Long id) {
        return this.baseMapper.selectRolesByEId(id);
    }

    @Override
    public void deleteRelation(Long id) {
        this.baseMapper.deleteRelation(id);
    }

    @Override
    public void insertRelation(Long eid, Long rid) {
        this.baseMapper.insertRelation(eid, rid);

    }

    @Override
    public Employee selectByUsername(String username) {
        return this.baseMapper.selectByUsername(username);
    }
}
